This puzzle is taken from an entertaining book of mathematical recreations called "The Moscow Puzzles" by Boris A. Kordemsky.